<?php

class Stat_memberApp extends BackendApp {

    var $_member_mod;
    var $search_arr;
    var $_statistics_day_mod;

    function __construct() {
        $this->Stat_memberApp();
    }

    function Stat_memberApp() {
        parent::__construct();
        $this->_member_mod = & m('member');
        $this->_statistics_day_mod = &m("statistics_day");
        import('datehelper.lib');
        import('statistics.lib');
        $this->import_resource(
                array(
                    'script' => 'highcharts/highcharts.js,statistics.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                    'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css')
        );
        $this->search_arr = $_REQUEST;
        //处理搜索时间
        if (in_array($_REQUEST['act'], array('newmember', 'analyze', 'scale', 'area','pvStatistics'))) {
            $this->search_arr = dealwithSearchTime($this->search_arr);
            //获得系统年份
            $year_arr = getSystemYearArr();
            //获得系统月份
            $month_arr = getSystemMonthArr();
            //获得本月的周时间段
            $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
            $this->assign('year_arr', $year_arr);
            $this->assign('month_arr', $month_arr);
            $this->assign('week_arr', $week_arr);
        }
        $this->assign('actionurl', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
        $this->assign('search_arr', $this->search_arr);
    }

    /**
     * 查询每月的周数组
     */
    public function getweekofmonth() {
        $year = $_GET['y'];
        $month = $_GET['m'];
        $week_arr = getMonthWeekArr($year, $month);
        echo json_encode($week_arr);
        die;
    }

    /**
     * 新增会员
     */
    function newmember() {
        if (!$this->search_arr['search_type']) {
            $this->search_arr['search_type'] = 'day';
        }
        $statlist = array(); //统计数据列表
        //新增总数数组
        $count_arr = array('up' => 0, 'curr' => 0);
        if ($this->search_arr['search_type'] == 'day') {
            //构造横轴数据
            for ($i = 0; $i < 24; $i++) {
                //统计图数据
                $curr_arr[$i] = 0; //今天
                $up_arr[$i] = 0; //昨天
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;

                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = $this->search_arr['day']['search_time'] + $i * 3600;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 3600;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = "$i";
            }
            $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
            $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
            //总计的查询时间
            $count_arr['seartime'] = ($stime + 86400) . '|' . $etime;

            $today_day = @date('d', $this->search_arr['day']['search_time']); //今天日期
            $yesterday_day = @date('d', $stime); //昨天日期


            $memberlist = $this->_member_mod->find(array(
                'fields' => 'count(*) as allnum ,DAY(FROM_UNIXTIME(reg_time)) as dayval,HOUR(FROM_UNIXTIME(reg_time)) as hourval',
                'conditions' => "reg_time>{$stime} AND reg_time < {$etime} group by dayval,hourval",
            ));
            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($today_day == $v['dayval']) {
                        $curr_arr[$v['hourval']] = intval($v['allnum']);
                        $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                    if ($yesterday_day == $v['dayval']) {
                        $up_arr[$v['hourval']] = intval($v['allnum']);
                        $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '昨天';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '今天';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('小时', '昨天', '今天', '同比');
            $this->assign('actionurl', 'index.php?app=stat_member&act=newmember&search_type=day&search_time=' . date('Y-m-d', $this->search_arr['day']['search_time']));
        }

        if ($this->search_arr['search_type'] == 'week') {
            $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = ($stime + 86400 * 7) . '|' . $etime;

            $up_week = @date('W', $stime); //上周
            $curr_week = @date('W', $etime); //本周
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = getSystemWeekArr();
                //统计表数据
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0]) + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }

            $memberlist = $this->_member_mod->find(array(
                'fields' => 'count(*) as allnum ,WEEKOFYEAR(FROM_UNIXTIME(reg_time)) as weekval,WEEKDAY(FROM_UNIXTIME(reg_time))+1 as dayofweekval ',
                'conditions' => "reg_time>{$stime} AND reg_time < {$etime} group by weekval,dayofweekval",
            ));


            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_week == intval($v['weekval'])) {
                        $up_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_week == $v['weekval']) {
                        $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }

            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('星期', '上周', '本周', '同比');
            $this->assign('actionurl', 'index.php?app=stat_member&act=newmember&search_type=week&searchweek_year=' . $this->search_arr['week']['current_year'] . '&searchweek_month=' . $this->search_arr['week']['current_month'] . '&searchweek_week=' . $this->search_arr['week']['current_week']);
        }


        if ($this->search_arr['search_type'] == 'month') {
            $stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01 -1 month");
            $etime = getMonthLastDay($this->search_arr['month']['current_year'], $this->search_arr['month']['current_month']) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01") . '|' . $etime;

            $up_month = date('m', $stime);
            $curr_month = date('m', $etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t', $stime);
            $curr_dayofmonth = date('t', $etime);
            $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;

            //构造横轴数据
            for ($i = 1; $i <= $x_max; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01") + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
                unset($tmp_montharr);
            }

            $memberlist = $this->_member_mod->find(array(
                'fields' => 'count(*) as allnum ,MONTH(FROM_UNIXTIME(reg_time)) as monthval,day(FROM_UNIXTIME(reg_time)) as dayval ',
                'conditions' => "reg_time>{$stime} AND reg_time < {$etime} group by monthval,dayval",
            ));

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_month == $v['monthval']) {
                        $up_arr[$v['dayval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']) {
                        $curr_arr[$v['dayval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('日期', '上月', '本月', '同比');
            $this->assign('actionurl', 'index.php?app=stat_member&act=newmember&search_type=month&searchmonth_year=' . $this->search_arr['month']['current_year'] . '&searchmonth_month=' . $this->search_arr['month']['current_month']);
        }

        //计算同比
        foreach ((array) $currlist_arr as $k => $v) {
            $tmp = array();
            $tmp['timetext'] = $v['timetext'];
            $tmp['seartime'] = $v['stime'] . '|' . $v['etime'];
            $tmp['currentdata'] = $v['val'];
            $tmp['updata'] = $uplist_arr[$k]['val'];
            $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
            $statlist['data'][] = $tmp;
        }
        //计算总结同比
        $count_arr['tbrate'] = getTb($count_arr['up'], $count_arr['curr']);

        //导出Excel
        if ($_GET['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            foreach ($statlist['headertitle'] as $v) {
                $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
            }
            //data
            foreach ($statlist['data'] as $k => $v) {
                $excel_data[$k + 1][] = array('data' => $v['timetext']);
                $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['updata']);
                $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['currentdata']);
                $excel_data[$k + 1][] = array('data' => $v['tbrate']);
            }
            $excel_data[count($statlist['data']) + 1][] = array('data' => '总计');
            $excel_data[count($statlist['data']) + 1][] = array('format' => 'Number', 'data' => $count_arr['up']);
            $excel_data[count($statlist['data']) + 1][] = array('format' => 'Number', 'data' => $count_arr['curr']);
            $excel_data[count($statlist['data']) + 1][] = array('data' => $count_arr['tbrate']);

            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('新增会员统计', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('新增会员统计', CHARSET) . date('Y-m-d-H', time()));
            exit();
        } else {
            //得到统计图数据
            $stat_arr['title'] = '新增会员统计';
            $stat_arr['yAxis'] = '新增会员数';
            $stat_json = getStatData_LineLabels($stat_arr);
            $this->assign('stat_json', $stat_json);
            $this->assign('statlist', $statlist);
            $this->assign('count_arr', $count_arr);
            $this->_curmenu('stat_member_newmember');
            $this->display('stat_member.newmember.html');
            exit;
        }
    }

    //pv统计数值
    function pvStatistics() {
        if (!$this->search_arr['search_type']) {
            $this->search_arr['search_type'] = 'day';
        }
        if(!$this->search_arr['keyword']){
            $this->search_arr['keyword'] = 'view_homepage';
        }

        $statlist = array(); //统计数据列表
        //新增总数数组
        $count_arr = array('up' => 0, 'curr' => 0);

        $typeList = $this->_statistics_day_mod->find(array("conditions"=>"cond = 'browse' group by keyword"));
        $this->assign('typeList',$typeList);


        if ($this->search_arr['search_type'] == 'day') {
            //构造横轴数据
            for ($i = 0; $i < 24; $i++) {
                //统计图数据
                $curr_arr[$i] = 0; //今天
                $up_arr[$i] = 0; //昨天
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;

                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = $this->search_arr['day']['search_time'] + $i * 3600;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 3600;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = "$i";
            }
            $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
            $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
            //总计的查询时间
            $count_arr['seartime'] = ($stime + 86400) . '|' . $etime;

            $today_day = @date('d', $this->search_arr['day']['search_time']); //今天日期
            $yesterday_day = @date('d', $stime); //昨天日期


            $memberlist = $this->_statistics_day_mod->find(array(
                'fields' => 'count as allnum ,DAY(FROM_UNIXTIME(time)) as dayval,HOUR(FROM_UNIXTIME(time)) as hourval',
                'conditions' => "cond = 'browse' AND keyword like '%".$this->search_arr['keyword']."%' AND time>{$stime} AND time < {$etime} group by dayval,hourval",
            ));
            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($today_day == $v['dayval']) {
                        $curr_arr[$v['hourval']] = intval($v['allnum']);
                        $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                    if ($yesterday_day == $v['dayval']) {
                        $up_arr[$v['hourval']] = intval($v['allnum']);
                        $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '昨天';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '今天';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('小时', '昨天', '今天', '同比');
            $this->assign('actionurl', 'index.php?app=stat_member&act=pvStatistics&search_type=day&search_time=' . date('Y-m-d', $this->search_arr['day']['search_time']));
        }
        if ($this->search_arr['search_type'] == 'week') {

            $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = ($stime + 86400 * 7) . '|' . $etime;

            $up_week = @date('W', $stime); //上周
            $curr_week = @date('W', $etime); //本周
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = getSystemWeekArr();
                //统计表数据
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0]) + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }


            $memberlist = $this->_statistics_day_mod->find(array(
                'fields' => 'count as allnum ,WEEKOFYEAR(FROM_UNIXTIME(time)) as weekval,WEEKDAY(FROM_UNIXTIME(time))+1 as dayofweekval ',
                'conditions' => "cond = 'browse' AND keyword like '%".$this->search_arr['keyword']."%' AND time>{$stime}  group by weekval,dayofweekval",
            ));
            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_week == intval($v['weekval'])) {
                        $up_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_week == $v['weekval']) {
                        $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }

            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('星期', '上周', '本周', '同比');
            $this->assign('actionurl', 'index.php?app=stat_member&act=pvStatistics&search_type=week&searchweek_year=' . $this->search_arr['week']['current_year'] . '&searchweek_month=' . $this->search_arr['week']['current_month'] . '&searchweek_week=' . $this->search_arr['week']['current_week']);
        }


        if ($this->search_arr['search_type'] == 'month') {
            $stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01 -1 month");
            $etime = getMonthLastDay($this->search_arr['month']['current_year'], $this->search_arr['month']['current_month']) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01") . '|' . $etime;

            $up_month = date('m', $stime);
            $curr_month = date('m', $etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t', $stime);
            $curr_dayofmonth = date('t', $etime);
            $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;

            //构造横轴数据
            for ($i = 1; $i <= $x_max; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01") + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
                unset($tmp_montharr);
            }

            $memberlist = $this->_statistics_day_mod->find(array(
                'fields' => 'count as allnum ,MONTH(FROM_UNIXTIME(time)) as monthval,day(FROM_UNIXTIME(time)) as dayval ',
                'conditions' => "cond = 'browse' AND keyword like '%".$this->search_arr['keyword']."%' AND time>{$stime} AND time < {$etime} group by monthval,dayval",
            ));

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_month == $v['monthval']) {
                        $up_arr[$v['dayval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']) {
                        $curr_arr[$v['dayval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('日期', '上月', '本月', '同比');
            $this->assign('actionurl', 'index.php?app=stat_member&act=pvStatistics&search_type=month&searchmonth_year=' . $this->search_arr['month']['current_year'] . '&searchmonth_month=' . $this->search_arr['month']['current_month']);
        }

        //计算同比
        foreach ((array) $currlist_arr as $k => $v) {
            $tmp = array();
            $tmp['timetext'] = $v['timetext'];
            $tmp['seartime'] = $v['stime'] . '|' . $v['etime'];
            $tmp['currentdata'] = $v['val'];
            $tmp['updata'] = $uplist_arr[$k]['val'];
            $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
            $statlist['data'][] = $tmp;
        }
        //计算总结同比
        $count_arr['tbrate'] = getTb($count_arr['up'], $count_arr['curr']);

        //导出Excel
        if ($_GET['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            foreach ($statlist['headertitle'] as $v) {
                $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
            }
            //data
            foreach ($statlist['data'] as $k => $v) {
                $excel_data[$k + 1][] = array('data' => $v['timetext']);
                $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['updata']);
                $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['currentdata']);
                $excel_data[$k + 1][] = array('data' => $v['tbrate']);
            }
            $excel_data[count($statlist['data']) + 1][] = array('data' => '总计');
            $excel_data[count($statlist['data']) + 1][] = array('format' => 'Number', 'data' => $count_arr['up']);
            $excel_data[count($statlist['data']) + 1][] = array('format' => 'Number', 'data' => $count_arr['curr']);
            $excel_data[count($statlist['data']) + 1][] = array('data' => $count_arr['tbrate']);

            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $info = $this->_statistics_day_mod->get(array("conditions"=>"keyword like '%".$this->search_arr['keyword']."%'"));
            $excel_obj->addWorksheet($excel_obj->charset($info['remark'].'浏览量pv统计', CHARSET));
            $excel_obj->generateXML($excel_obj->charset($info['remark'].'浏览量pv统计', CHARSET) . date('Y-m-d-H', time()));
            exit();
        } else {
            //得到统计图数据
            $stat_arr['title'] = '浏览量PV统计';
            $stat_arr['yAxis'] = '浏览量数';
            $stat_json = getStatData_LineLabels($stat_arr);
            $this->assign('stat_json', $stat_json);
            $this->assign('statlist', $statlist);
            $this->assign('count_arr', $count_arr);
            $this->_curmenu('stat_member_pystatistics');
            $this->display('stat_member.pystatistics.html');
            exit;
        }
    }

    function showmember() {
        $conditions = "1=1";
        if (in_array($_GET['type'], array('newbyday', 'newbyweek', 'newbymonth'))) {
            $actionurl = 'index.php?app=stat_member&act=showmember&type=newbyday&t=' . $_GET['t'];
            $searchtime_arr = explode('|', $_GET['t']);
            $conditions = "reg_time>{$searchtime_arr[0]} AND reg_time < {$searchtime_arr[1]}";
        }
        if ($this->search_arr['exporttype'] == 'excel') {
            $member_list = $this->_member_mod->find(array(
                'conditions' => $conditions,
                    )
            );
        } else {
            $page = $this->_get_page(10);
            $member_list = $this->_member_mod->find(array(
                'conditions' => $conditions,
                'limit' => $page['limit'],
                'count' => true,
            ));
            $page['item_count'] = $this->_member_mod->getCount();
            $this->_format_page($page);
            $this->assign('page_info', $page);
        }
        if (is_array($member_list)) {
            foreach ($member_list as $k => $v) {
                $member_list[$k]['reg_time'] = $v['reg_time'] ? date('Y-m-d H:i:s', $v['reg_time']) : '';
                $member_list[$k]['last_login'] = $v['last_login'] ? date('Y-m-d H:i:s', $v['last_login']) : '';
            }
        }
        //导出Excel
        if ($this->search_arr['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '用户名');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '注册时间');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '登录次数');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '最后登录时间');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '手机号码');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '可用积分');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '总积分');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '可用资金');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '冻结资金');
            //data
            foreach ($member_list as $k => $v) {
                $excel_data[$k + 1][] = array('data' => $v['user_name']);
                $excel_data[$k + 1][] = array('data' => $v['reg_time']);
                $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['logins']);
                $excel_data[$k + 1][] = array('data' => $v['last_login'] . '(IP:' . $v['last_ip'] . ')');
                $excel_data[$k + 1][] = array('data' => $v['phone_mob']);
                $excel_data[$k + 1][] = array('data' => $v['integral']);
                $excel_data[$k + 1][] = array('data' => $v['total_integral']);
                $excel_data[$k + 1][] = array('data' => $v['money']);
                $excel_data[$k + 1][] = array('data' => $v['money_dj']);
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('新增会员', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('新增会员', CHARSET) . date('Y-m-d-H', gmtime()));
            exit();
        }
        
        $this->assign('member_list', $member_list);
        $this->assign('actionurl', $actionurl);
        $this->_curmenu('stat_member_newmember');
        $this->display('stat_member.showmember.html');
    }

    function analyze() {
        if (!$this->search_arr['search_type']) {
            $this->search_arr['search_type'] = 'day';
        }
        //构造横轴数据
        for ($i = 1; $i <= 15; $i++) {
            //横轴
            $stat_arr['xAxis']['categories'][] = $i;
        }
        $stat_arr['title'] = '买家排行Top15';
        $stat_arr['legend']['enabled'] = false;

        //获得搜索的开始时间和结束时间
        $searchtime_arr = getStarttimeAndEndtime($this->search_arr);
        $conditions = "1=1 ";
        if (!empty($searchtime_arr)) {
            $conditions .= " AND add_time>{$searchtime_arr[0]} AND add_time < {$searchtime_arr[1]} ";
        }
        $conditions .= " group by buyer_id ";

        //下单量
        $order_mod = & m('order');
        $ordernum_listtop15 = $order_mod->find(array(
            'fields' => ' buyer_id, buyer_name, count(*) as ordernum ',
            'conditions' => $conditions,
            'order' => 'ordernum desc,buyer_id desc',
            'limit' => 15,
        ));

        $ordernum_listtop15 = array_merge($ordernum_listtop15);

        $stat_ordernum_arr = $stat_arr;
        $stat_ordernum_arr['series'][0]['name'] = '下单量';
        $stat_ordernum_arr['series'][0]['data'] = array();
        for ($i = 0; $i < 15; $i++) {
            $stat_ordernum_arr['series'][0]['data'][] = array('name' => strval($ordernum_listtop15[$i]['buyer_name']), 'y' => intval($ordernum_listtop15[$i]['ordernum']));
        }
        $stat_ordernum_arr['yAxis'] = '下单量';
        $statordernum_json = getStatData_Column2D($stat_ordernum_arr);
        unset($stat_ordernum_arr);
        $this->assign('statordernum_json', $statordernum_json);
        $this->assign('ordernum_listtop15', $ordernum_listtop15);


        //下单商品件数
        $order_goods_mod = & m('ordergoods');
        $goodsnum_listtop15 = $order_goods_mod->find(array(
            'fields' => ' buyer_id, buyer_name, sum(quantity) as goodsnum ',
            'conditions' => $conditions,
            'order' => 'goodsnum desc,buyer_id desc',
            'join' => 'belongs_to_order',
            'limit' => 15,
        ));
        $goodsnum_listtop15 = array_merge($goodsnum_listtop15);

        $stat_goodsnum_arr = $stat_arr;
        $stat_goodsnum_arr['series'][0]['name'] = '下单商品件数';
        $stat_goodsnum_arr['series'][0]['data'] = array();
        for ($i = 0; $i < 15; $i++) {
            $stat_goodsnum_arr['series'][0]['data'][] = array('name' => strval($goodsnum_listtop15[$i]['buyer_name']), 'y' => intval($goodsnum_listtop15[$i]['goodsnum']));
        }
        $stat_goodsnum_arr['yAxis'] = '下单商品件数';
        $statgoodsnum_json = getStatData_Column2D($stat_goodsnum_arr);
        unset($stat_goodsnum_arr);
        $this->assign('statgoodsnum_json', $statgoodsnum_json);
        $this->assign('goodsnum_listtop15', $goodsnum_listtop15);

        //下单金额
        $orderamount_listtop15 = $order_mod->find(array(
            'fields' => ' buyer_id, buyer_name, sum(order_amount) as orderamount ',
            'conditions' => $conditions,
            'order' => 'orderamount desc,buyer_id desc',
            'limit' => 15,
        ));
        $orderamount_listtop15 = array_merge($orderamount_listtop15);

        $stat_orderamount_arr = $stat_arr;
        $stat_orderamount_arr['series'][0]['name'] = '下单金额';
        $stat_orderamount_arr['series'][0]['data'] = array();
        for ($i = 0; $i < 15; $i++) {
            $stat_orderamount_arr['series'][0]['data'][] = array('name' => strval($orderamount_listtop15[$i]['buyer_name']), 'y' => floatval($orderamount_listtop15[$i]['orderamount']));
        }
        $stat_orderamount_arr['yAxis'] = '下单金额';
        $statorderamount_json = getStatData_Column2D($stat_orderamount_arr);
        unset($stat_orderamount_arr);
        $this->assign('statorderamount_json', $statorderamount_json);
        $this->assign('orderamount_listtop15', $orderamount_listtop15);
        $this->assign('searchtime', implode('|', $searchtime_arr));
        $this->_curmenu('stat_member_analyze');
        $this->display('stat_member.analyze.html');
    }

    function scale() {
        if (!$this->search_arr['search_type']) {
            $this->search_arr['search_type'] = 'day';
        }
        $statlist = array(); //统计数据列表
        //获得搜索的开始时间和结束时间
        $searchtime_arr = getStarttimeAndEndtime($this->search_arr);

        $conditions = "complete=1";
        if (!empty($searchtime_arr)) {
            $conditions .= " AND add_time>{$searchtime_arr[0]} AND add_time < {$searchtime_arr[1]} ";
        }
        $conditions .= " group by user_id ";

        if (trim($this->search_arr['orderby'])) {
            $orderby = trim($this->search_arr['orderby']);
        } else {
            $orderby = 'predincrease desc';
        }
        $orderby .= ',user_id desc';

        $account_log_mod = & m('account_log');
        if ($_GET['exporttype'] == 'excel') {
            $statlist = $account_log_mod->find(array(
                'fields' => "user_id, user_name, sum(if(money_flow='income',money,0)) as predincrease, sum(if(money_flow='outlay',money,0)) as predreduce",
                'conditions' => $conditions,
                'order' => $orderby,
                'limit' => '',
            ));
        } else {
            $page = $this->_get_page(10);
            $statlist = $account_log_mod->find(array(
                'fields' => "user_id, user_name, sum(if(money_flow='income',money,0)) as predincrease, sum(if(money_flow='outlay',money,0)) as predreduce",
                'conditions' => $conditions,
                'order' => $orderby,
                'limit' => $page['limit'],
                'count' => true,
            ));
            $page['item_count'] = $account_log_mod->getCount();
            $this->_format_page($page);
            $this->assign('page_info', $page);
        }


        //导出Excel
        if ($this->search_arr['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '会员名称');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '增预存款');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '减预存款');
            //data
            foreach ($statlist as $k => $v) {
                $excel_data[$k + 1][] = array('data' => $v['user_name']);
                $excel_data[$k + 1][] = array('data' => $v['predincrease']);
                $excel_data[$k + 1][] = array('data' => $v['predreduce']);
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('会员规模分析', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('会员规模分析', CHARSET) . date('Y-m-d-H', time()));
            exit();
        }
        $this->assign('statlist', $statlist);
        $this->_curmenu('stat_member_scale');
        $this->display('stat_member.scale.html');
    }

    function _get_member_submenu() {
        $submenus = array(
            array(
                'name' => 'stat_member_newmember',
                'url' => 'index.php?app=stat_member&amp;act=newmember',
            ),
            array(
                'name' => 'stat_member_analyze',
                'url' => 'index.php?app=stat_member&amp;act=analyze',
            ),
            array(
                'name' => 'stat_member_scale',
                'url' => 'index.php?app=stat_member&amp;act=scale',
            ),
        );
        return $submenus;
    }

}

?>
